package com.xinyu.task;

import com.xinyu.service.IDailyService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * Periodically realigns Daily(issue) state with its bound Task to recover from manual edits.
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class TaskDailyStateReconcileJob {

    private final IDailyService dailyService;

    @Scheduled(fixedDelayString = "${task.state-sync.interval-ms:60000}")
    public void reconcile() {
        int updated = dailyService.syncStateWithTask();
        if (updated > 0) {
            log.info("Reconciled {} Daily records to match task states.", updated);
        } else {
            log.trace("No task/Daily state drift detected in this interval.");
        }
    }
}
